package com.djl.mvc.controller;

import com.djl.crowd.entity.Admin;
import com.djl.crowd.util.CrowdConstant;
import com.djl.service.AdminService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 管理员维护
 *
 * @Author le
 * @Date 2021/11/8 10:06
 * @Version 1.0
 */

@Slf4j
@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PreAuthorize("hasRole('programmer') or hasRole('店长') or hasRole('总经理')")
    @PostMapping("/admin/update.html")
    public String update(Admin admin, @RequestParam("pageNum") Integer pageNum, @RequestParam("keyword") String keyword) {
        adminService.update(admin);
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }


    @PreAuthorize("hasRole('programmer') or hasRole('店长') or hasRole('总经理')")
    @GetMapping("/admin/to/update/page.html")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model) {
        // 根据id查询管理员信息
        Admin admin = adminService.getAdminById(adminId);

        model.addAttribute(CrowdConstant.ATTR_NAME_EDIT_ADMIN, admin);

        return "admin-edit";
    }

    @PreAuthorize("hasRole('programmer') or hasRole('店长') or hasRole('总经理')")
    @RequestMapping("/admin/save.html")
    public String save(Admin admin) {
        adminService.save(admin);
        return "redirect:/admin/get/page.html?pageNum=" + Integer.MAX_VALUE;
    }


    /**
     * 删除管理员信息
     * `
     *
     * @param adminId 管理员id
     * @param pageNum
     * @param keyword
     * @return
     */
    @PreAuthorize("hasRole('programmer') or hasRole('店长') or hasRole('总经理')")
    @GetMapping("admin/remove/{adminId}/{pageNum}/{keyword}.html")
    public String remove(@PathVariable("adminId") Integer adminId,
                         @PathVariable("pageNum") Integer pageNum,
                         @PathVariable("keyword") String keyword) {
        adminService.remove(adminId);
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }

    /**
     * 分页显示管理员信息
     *
     * @param keyword  模糊查询条件
     * @param pageNum  当前页码，默认1
     * @param pageSize 每页大小 默认5
     * @param model
     * @return
     */
    @PreAuthorize("hasRole('programmer') or hasRole('店长') or hasRole('总经理')")
    @RequestMapping("admin/get/page.html")
    public String getPageInfo(@RequestParam(value = "keyword", defaultValue = "") String keyword,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                              ModelMap model) {
        PageInfo<Admin> adminPageInfo = adminService.getPageByKeyword(keyword, pageNum, pageSize);
        model.addAttribute(CrowdConstant.ATTR_NAME_PAGEINFO, adminPageInfo);
        return "admin-page";
    }

    /**
     * 登出操作
     *
     * @param session
     * @return
     */
    @GetMapping("admin/do/logout.html")
    public String doLogout(HttpSession session) {
        //强制session失效
        session.invalidate();
        return "redirect:/admin/to/login/page.html";
    }


    /**
     * 管理员登录
     *
     * @param loginAcct 用户账号
     * @param userPswd  用户密码
     * @param session
     * @return
     */
    @PostMapping("admin/do/login.html")
    public String doLogin(@RequestParam("loginAcct") String loginAcct,
                          @RequestParam("userPswd") String userPswd,
                          HttpSession session) {
        // 查询管理员信息
        Admin admin = adminService.getAdminByLoginAcct(loginAcct, userPswd);
        // 保存到session域中
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);

        return "redirect:/admin/to/main/page.html";
    }

}
